package com.example.pclimb.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.pclimb.model.TravelGuide;
import com.example.pclimb.repository.TravelGuideRepository;
import com.example.pclimb.repository.UserRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * 旅游攻略视图模型，负责处理旅游攻略相关的UI数据
 */
public class TravelGuideViewModel extends AndroidViewModel {
    private TravelGuideRepository repository;
    private LiveData<List<TravelGuide>> allTravelGuides;
    private MutableLiveData<List<TravelGuide>> topViewedGuides = new MutableLiveData<>();
    private LiveData<List<TravelGuide>> topLikedGuides;
    private MutableLiveData<TravelGuide> selectedGuide = new MutableLiveData<>();

    public TravelGuideViewModel(@NonNull Application application) {
        super(application);
        repository = new TravelGuideRepository(application);
        allTravelGuides = repository.getAllTravelGuides();
        topLikedGuides = repository.getTopLikedGuides();
        loadTopViewedGuides();
    }

    public void insert(TravelGuide travelGuide, UserRepository.Callback<Long> callback) {
        repository.insert(travelGuide, callback);
    }

    public void update(TravelGuide travelGuide) {
        repository.update(travelGuide);
    }

    public void delete(TravelGuide travelGuide) {
        repository.delete(travelGuide);
    }

    public LiveData<List<TravelGuide>> getAllTravelGuides() {
        return allTravelGuides;
    }

    public LiveData<TravelGuide> getTravelGuideById(int guideId) {
        return repository.getTravelGuideById(guideId);
    }

    public LiveData<List<TravelGuide>> getTravelGuidesByAuthor(int authorId) {
        return repository.getTravelGuidesByAuthor(authorId);
    }

    public LiveData<List<TravelGuide>> getTravelGuidesByLocation(String location) {
        return repository.getTravelGuidesByLocation(location);
    }

    public LiveData<List<TravelGuide>> searchTravelGuides(String keyword) {
        return repository.searchTravelGuides(keyword);
    }

    public void incrementViewCount(int guideId) {
        repository.incrementViewCount(guideId);
    }

    public void incrementLikeCount(int guideId) {
        repository.incrementLikeCount(guideId);
    }

    public LiveData<List<TravelGuide>> getTopViewedGuides() {
        return topViewedGuides;
    }

    public LiveData<List<TravelGuide>> getTopLikedGuides() {
        return topLikedGuides;
    }

    public void setSelectedGuide(TravelGuide guide) {
        selectedGuide.setValue(guide);
    }

    public LiveData<TravelGuide> getSelectedGuide() {
        return selectedGuide;
    }

    private void loadTopViewedGuides() {
        // 从repository获取数据而非使用硬编码数据
        repository.getTopViewedGuides().observeForever(guides -> {
            if (guides != null && !guides.isEmpty()) {
                topViewedGuides.setValue(guides);
            } else {
                // 如果没有数据，使用示例数据
                List<TravelGuide> defaultGuides = new ArrayList<>();
                defaultGuides.add(new TravelGuide(1, "黄山游玩攻略", "这是一篇关于黄山的旅游攻略...", "黄山", ""));
                defaultGuides.add(new TravelGuide(2, "泰山一日游", "这是一篇关于泰山的旅游攻略...", "泰山", ""));
                defaultGuides.add(new TravelGuide(3, "华山险峰攀登指南", "这是一篇关于华山的旅游攻略...", "华山", ""));
                topViewedGuides.setValue(defaultGuides);
            }
        });
    }

    public void getGuideById(long id) {
        // TODO: 根据ID获取详细攻略
    }

    public void createGuide(TravelGuide guide) {
        // TODO: 创建新攻略
    }
    
    // 刷新所有旅游攻略
    public void refreshAllTravelGuides() {
        allTravelGuides = repository.getAllTravelGuides();
    }
    
    // 刷新热门攻略
    public void refreshTopViewedGuides() {
        loadTopViewedGuides();
    }
}